<!--
  This is template for hugo menus, accepts MenuEntity as context
  https://gohugo.io/variables/menus/
-->
{{ $currentPage := .}}
{{ if .Site.Menus.header }}
  <ul>
    {{ $activePage := partial "get-active-page" (dict "Menu" .Site.Menus.header "CurrentPage" $currentPage)}}
    {{ range .Site.Menus.header }}
    {{ $page := partial "get-page-by-menu" (dict "MenuEntry" . "Site" $currentPage.Site )}}
    <li class='{{ if eq $activePage $page }}active{{ end }}'>
      <a href="{{ .URL }}"  {{ if or (not $page) (ne $page.Section $currentPage.Section) }}target="_blank" rel="noopener"{{ end }}>
        {{- .Pre -}}
        {{ if .Name }}
          {{ .Name }}
        {{ else }}
          {{ partial "docs/title" $page }}
        {{ end }}
        {{- .Post -}}
      </a>
    </li>
    {{ end }}
  </ul>
{{ end }}


{{ define "partials/get-active-page" }}
  {{ $activePage := false }}
  {{ range .Menu }}
    {{ $page := partial "get-page-by-menu" (dict "MenuEntry" . "Site" $.CurrentPage.Site )}}
    {{  if and $page.IsSection ($page.IsAncestor $.CurrentPage) }}
      {{ if $activePage }}
        {{ if $activePage.IsAncestor $page}}
          {{ $activePage = $page }}
        {{ end }}
      {{ else }}
        {{ $activePage = $page }}
      {{ end }}
    {{ end }}
  {{ end }}
  {{ return $activePage }}
{{ end }}

{{ define "partials/get-page-by-menu" }}
  {{ $page := .MenuEntry.Page }}
  {{ if not $page }}
    {{ $page = .Site.GetPage .MenuEntry.URL }}
  {{ end }}
  {{ return $page }}
{{ end }}